var router = require('koa-router')();
const tasksModel = require('../models/tasks');

router.prefix('/api/tasks');

router.post('/', async (ctx) => {
    const { text, user_id } = ctx.request.body;
    ctx.body = tasksModel.addTask(text, user_id);
});

router.delete('/:id', async (ctx) => {
    const { id } = ctx.params;
    ctx.body = tasksModel.removeById(id);
});

router.put('/:id', async (ctx) => {
    const { id } = ctx.params;
    const { text } = ctx.request.body;
    ctx.body = tasksModel.updateById(id, text);
});

router.get('/:id', async (ctx) => {
    const { id } = ctx.params;
    ctx.body = await tasksModel.getTaskById(id);
});

module.exports = router;